//
//  MainViewController.m
//  WealthManager
//
//  Created by rimi on 15/9/28.
//  Copyright (c) 2015年 魏攀. All rights reserved.
//

#import "MainViewController.h"

static const NSTimeInterval animationDuration_ = 0.28;

@interface MainViewController ()<UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIPanGestureRecognizer * panGesture;/**< 拖动手势 */
@property (nonatomic, strong) UITapGestureRecognizer * tapGesture;/**< 单击手势 */

- (void)initializeDataSource; /**< 初始化数据源 */
- (void)initializeAppearance;/**< 初始化界面 */

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeAppearance];
}
#pragma mark - private methods
- (void)initializeDataSource {
    self.isOpen = [[NSUserDefaults standardUserDefaults]boolForKey:@"isOpen"];
}
- (void)initializeAppearance {
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addGestureRecognizer:self.panGesture];
    [self.view addGestureRecognizer:self.tapGesture];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    BOOL result = NO;
    
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        result = YES;
    }
    
    return result;
}
#pragma mark - responds events
- (void)action_onPanGesture:(UIPanGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan) {
        CGPoint velocity = [gesture velocityInView:self.view];
        if (velocity.x < 0) {
            return;
        }
    }
    else if (gesture.state == UIGestureRecognizerStateChanged) {
        CGPoint tranlate = [gesture translationInView:self.view];
        if (!_isOpen) {
            /**< 任意时刻位置 */
            if (tranlate.x < 0) {
                tranlate.x = 0;/**< 判断是否拉过界 */
            }
            if (tranlate.x > 240) {
                tranlate.x = 240;
            }
            self.view.frame = CGRectMake(60+tranlate.x, 0, ORIGIN_WIDTH-60-tranlate.x, ORIGIN_HEIGHT-20);
        } else {
            if (tranlate.x < -165) {
                tranlate.x = -165;/**< 判断是否拉过界 */
            }
            if (tranlate.x > 75) {
                tranlate.x = 75;
            }
            self.view.frame = CGRectMake(225+tranlate.x, 0, ORIGIN_WIDTH-225-tranlate.x, ORIGIN_HEIGHT-20);
        }
        
    }
    else if (gesture.state == UIGestureRecognizerStateEnded) {
        CGPoint velocity = [gesture velocityInView:self.view];
        if (velocity.x > 0) {
            [self openLeftViewController];
        } else {
            [self closeLeftViewController];
        }
    }
}
- (void)action_onTapGesture:(UIScreenEdgePanGestureRecognizer *)gesture {
    if (!_isOpen) {
        return;
    }
    [self closeLeftViewController];
}
#pragma mark - interface methods
- (void)openLeftViewController {
    [UIView animateWithDuration:animationDuration_ animations:^{
        self.view.frame = CGRectMake(225, 0, ORIGIN_WIDTH-225, ORIGIN_HEIGHT-20);
    }];
    self.isOpen = YES;
    [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"isOpen"];
}
- (void)closeLeftViewController {
    [UIView animateWithDuration:animationDuration_ animations:^{
        self.view.frame = CGRectMake(60, 0, ORIGIN_WIDTH-60, ORIGIN_HEIGHT-20);
    }];
    self.isOpen = NO;
    [[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"isOpen"];
}
#pragma mark - getter
- (UIPanGestureRecognizer *)panGesture
{
    if (!_panGesture) {
        _panGesture = ({
            UIPanGestureRecognizer * gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(action_onPanGesture:)];
            gesture;
        });
    }
    return _panGesture;
}
- (UITapGestureRecognizer *)tapGesture {
    if (!_tapGesture) {
        _tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(action_onTapGesture:)];
    }
    return _tapGesture;
}
@end
